package com.woniu.atms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.atms.entity.G5Route;
import com.woniu.atms.entity.G5Schedule;
import com.woniu.atms.entity.G5Site;
import com.woniu.atms.mapper.ScheduleMapper;
import com.woniu.atms.mapper.SiteMapper;
import com.woniu.atms.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 班次操作服务接口实现类
 */
@Service("scheduleService")
public class ScheduleServiceImpl implements ScheduleService {
//    @Autowired
//    private RedisTemplate redisTemplate;
    @Autowired
    private ScheduleMapper scheduleMapper;
//    @Autowired
//    private SiteMapper siteMapper;

    @Override
    public PageInfo<G5Schedule> findAll(Integer pageSize, Integer pageNum) {
        //设置pageHelper属性
        PageHelper.startPage(pageNum,pageSize);
        //获取list对象
        List<G5Schedule> schedules = scheduleMapper.selectList(null);
        //返回pageInfo对象
        return new PageInfo<>(schedules);
    }

    @Override
    public G5Schedule findById(Integer id) {
        //直接调用返回
        return scheduleMapper.selectById(id);
    }

    @Override
    public void add(G5Schedule schedule) {
        //直接添加
        scheduleMapper.insert(schedule);
    }

    @Override
    public void updateStatusById(Integer id, String status) {
        //新建对象并设置内容
        G5Schedule schedule = new G5Schedule();
        schedule.setId(id);
        schedule.setStatus(status);
        //更新数据
        scheduleMapper.updateById(schedule);
    }

    @Override
    public void updatePriceById(Integer id, BigDecimal price) {
        //新建对象并设置内容
        G5Schedule schedule = new G5Schedule();
        schedule.setId(id);
        schedule.setPrice(price);
        //更新数据
        scheduleMapper.updateById(schedule);
    }

    @Override
    public PageInfo<G5Schedule> findByStatus(String status, Integer pageSize, Integer pageNum) {
        //设置pageHelper属性
        PageHelper.startPage(pageNum,pageSize);
        //新建查询对象并设置查询对比条件
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("status", status);
        //获取list对象
        List<G5Schedule> schedules = scheduleMapper.selectList(queryWrapper);
        //返回pageInfo对象
        return new PageInfo<>(schedules);
    }

    @Override
    public List<G5Schedule> findByRouteId(Integer routeId) {
        //新建查询对象并设置查询对比条件
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("route_id", routeId);
        //直接返回查询结果
        return scheduleMapper.selectList(queryWrapper);
    }

    @Override
    public List<G5Schedule> findByRouteList(List<Integer> ids) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("route_id",ids);
        return scheduleMapper.selectList(queryWrapper);
    }


    /**
     * 使用起始终点模糊查询所有班次（废弃）
     * @param start 起点
     * @param end 终点
     * @return 返回班次列表
     * @deprecated 已弃用
     */
    @Deprecated
    public List<G5Schedule> findByStartEnd(String start, String end) {
        /*//新建结果数组
        List<G5Schedule> resultList = new ArrayList<>();
        //查询匹配站点
        //新建起始站点匹配数组
        List<G5Site> startSiteList = new ArrayList<>();
        //设置初始信号
        boolean firstSign = true;
        //对每个字符进行查询，合并查询结果
        for(char sub:start.toCharArray()){
            //生成子字符串
            String subStr = String.valueOf(sub);
            //初始化本次查询结果
            List<G5Site> tp = null;
            if(" ".equals(subStr) || "".equals(subStr))
                continue;
            //到缓存中查询
            tp = (List<G5Site>)redisTemplate.opsForValue().get("scheduleService_findByStartEnd_strFz_"+subStr);
            //如果缓存没有数据则查询数据库
            if(tp == null){
                tp = siteMapper.findSiteByFuzzyNameOrAddress(subStr);
                //如果查询不到，则设置为空对象数组
                if(tp == null)
                    tp = new ArrayList<>();
                //增加到缓存
                redisTemplate.opsForValue().set("scheduleService_findByStartEnd_strFz_"+subStr,tp);
            }
            //如果是初始状态则直接并集,否则为求交集
            if(firstSign){
                startSiteList.addAll(tp);
            }else{
                startSiteList.retainAll(tp);
            }
            //如果结果已为空数组，则返回空数组
            if(startSiteList.size() == 0)
                return new ArrayList<>();
        }

        //新建终点匹配数组,用相同方式查询
        List<G5Site> endSiteList = new ArrayList<>();
        firstSign = true;
        for(char sub:end.toCharArray()){
            String subStr = String.valueOf(sub);
            List<G5Site> tp = null;
            if(" ".equals(subStr) || "".equals(subStr))
                continue;
            tp = (List<G5Site>)redisTemplate.opsForValue().get("scheduleService_findByStartEnd_strFz_"+subStr);
            if(tp == null){
                tp = siteMapper.findSiteByFuzzyNameOrAddress(subStr);
                if(tp == null)
                    tp = new ArrayList<>();
                //增加到缓存
                redisTemplate.opsForValue().set("scheduleService_findByStartEnd_strFz_"+subStr,tp);
            }
            if(firstSign){
                endSiteList.addAll(tp);
            }else{
                endSiteList.retainAll(tp);
            }
            if(endSiteList.size() == 0)
                return new ArrayList<>();
        }*/
        return null;
    }
}
